
package com.dao.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bean.Customer;
import com.dao.CustomerDao;
import com.utils.JdbcUtils;

public class CustomerDaoImpl implements CustomerDao {

	public boolean add(Customer customer) {
		// 拿到连接对象
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement pstmt = null;
		// 创建预处理命令对象
		int n = 0;
		try {
			pstmt = conn
					.prepareStatement("insert into "
							+ "customer(id,name,password,type,phone,email) values(?,?,?,?,?,?)");
			// 指定?的值
			pstmt.setString(1, customer.getId());
			pstmt.setString(2, customer.getName());
			pstmt.setString(3, customer.getPassword());
			pstmt.setString(4, customer.getType());
			pstmt.setString(5, customer.getPhone());
			pstmt.setString(6, customer.getEmail());

			// 执行sql语句
			n = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.release(null, pstmt, conn);
		}
		return n > 0 ? true : false;
	}
	

	public boolean update(Customer customer) {
		// 拿到连接对象
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement pstmt = null;
		// 创建预处理命令对象
		int n = 0;
		try {
			pstmt = conn
					.prepareStatement("update "
							+ "customer set name=?,password=?,type=?,phone=?,email=? where id = ?");
			// 指定?的值

			
			pstmt.setString(1, customer.getName());
			pstmt.setString(2, customer.getPassword());
			pstmt.setString(3, customer.getType());
			pstmt.setString(4, customer.getPhone());
			pstmt.setString(5, customer.getEmail());
			pstmt.setString(6, customer.getId());

			
			// 执行sql语句
			n = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.release(null, pstmt, conn);
		}
		return n > 0 ? true : false;
	}

	public boolean delete(String id) {
		// 拿到连接对象
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement pstmt = null;
		// 创建预处理命令对象
		int n = 0;
		try {
			pstmt = conn.prepareStatement("delete from customer where id = ?");
			// 指定?的值
			pstmt.setString(1, id);
			// 执行sql语句
			n = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.release(null, pstmt, conn);
		}
		return n > 0 ? true : false;
	}

	public List<Customer> getAllCustomer() {
		// 拿到连接对象
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Customer> list = new ArrayList<Customer>();
		// 创建预处理命令对象
		try {
			pstmt = conn
					.prepareStatement("select id,name,password,type,phone,email from customer");

			// 执行sql语句
			rs = pstmt.executeQuery();
			while (rs.next()) {
				// 封装数据
				Customer c = new Customer();
				try {
					String id = URLEncoder.encode(rs.getString("id"), "UTF-8");
					c.setId(id);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				c.setName(rs.getString("name"));
				c.setPassword(rs.getString("password"));
				c.setType(rs.getString("type"));
				c.setPhone(rs.getString("phone"));
				c.setEmail(rs.getString("email"));
				
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(rs, pstmt, conn);
		}
		return list;
	}

	public Customer findCustomerById(String id) {
		// 拿到连接对象
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		// 创建预处理命令对象
		try {
			pstmt = conn
					.prepareStatement("select id,name,password,type,phone,email from customer where id = '"
							+ id + "'");

			// 执行sql语句
			rs = pstmt.executeQuery();
			if (rs.next()) {
				// 封装数据
				Customer c = new Customer();

				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setPassword(rs.getString("password"));
				c.setType(rs.getString("type"));
				c.setPhone(rs.getString("phone"));
				c.setEmail(rs.getString("email"));

				return c;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(rs, pstmt, conn);
		}
		return null;
	}

	public List<Customer> getPageList(int currentPageIndex, int count) {
		// 拿到连接对象
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Customer> list = new ArrayList<Customer>();
		// 创建预处理命令对象
		try {
			pstmt = conn
					.prepareStatement("select id,name,password,type,phone,email from customer limit ?,?");
			// 指定?的值
			pstmt.setInt(1, (currentPageIndex - 1) * count);
			pstmt.setInt(2, count);
			// 执行sql语句
			rs = pstmt.executeQuery();
			while (rs.next()) {
				// 封装数据
				Customer c = new Customer();
				try {
					String id = URLEncoder.encode(rs.getString("id"), "UTF-8");
					c.setId(id);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				c.setName(rs.getString("name"));
				c.setPassword(rs.getString("password"));
				c.setType(rs.getString("type"));
				c.setPhone(rs.getString("phone"));
				c.setEmail(rs.getString("email"));

				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(null, pstmt, conn);
		}
		return list;
	}

	public int getTotalCount() {
		// 拿到连接对象
		Connection conn = JdbcUtils.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null ;
		// 创建预处理命令对象
		try {
			pstmt = conn
					.prepareStatement("select count(*) from customer");
			
			// 执行sql语句
			rs = pstmt.executeQuery() ;
			if(rs.next())   //执行第一条记录
				return rs.getInt(1) ;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(null, pstmt, conn);
		}
		return 0;
	}

}

